package com.wgj.android.dialog;

import android.animation.ObjectAnimator;
import android.app.DialogFragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiaai.xui.utils.XDensityUtils;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.utils.imageload.XImage;
import com.jiaai.xui.view.widget.CircleImageView;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.events.AdaverHongBaoEvent;
import com.wgj.android.events.Adver;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.ui.activity.HBDetialActivity;
import com.wgj.android.utils.CacheUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/1/31.
 */

public class OpenAdverHongBaoDialog extends AppCompatDialogFragment {

    private Adver adver;
    @BindView(R.id.head)
    CircleImageView head;
    @BindView(R.id.iv_open)
    ImageView ivOpen;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.money)
    TextView money;
    
    public static OpenAdverHongBaoDialog getInstael( Adver adver) {
        OpenAdverHongBaoDialog qdDialog = new OpenAdverHongBaoDialog();
        Bundle bundle = new Bundle();
        bundle.putSerializable("adver",adver);
        qdDialog.setArguments(bundle);
        return qdDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adver= (Adver) getArguments().getSerializable("adver");
        setStyle(DialogFragment.STYLE_NORMAL, R.style.dialog_setting);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.open_adver_hongbao_dialog, container, false);
        ButterKnife.bind(this, view);
        initData();


        return view;
    }

    private void initData() {
        XImage.getInstance().load(head, CacheUtils.getMember().getAvater());
        tvName.setText(CacheUtils.getMember().getM_nick());
    }

    @OnClick(R.id.iv_open)
    public void onOpen(View v) {
        Glide.with( this ).load( R.mipmap.huob ).asGif().into( ivOpen );
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                XHttp.obtain().post(WGJAPI.openAdverHongBao(adver.getId()), null, new HttpCallBack<String>() {
                    @Override
                    public void onSuccess(String s) {
                        adver.setIs_get(1);
                        ivOpen.setVisibility(View.GONE);
                        money.setText("恭喜您获得"+s+"元");
                        money.setVisibility(View.VISIBLE);
                        EventBus.getDefault().post(new AdaverHongBaoEvent(true));
                    }
                    @Override
                    public void onFailed(int i, String s, boolean b) {
                        XToast.error(s);
                    }
                });
            }
        },2000);
    }

    @OnClick(R.id.closeBut)
    public  void  closeBut(View view){
         dismiss();
    }
    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCanceledOnTouchOutside(true); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = XDensityUtils.dp2px(300f);
        lp.gravity= Gravity.CENTER;
        lp.height =  XDensityUtils.dp2px(380f);
        window.setAttributes(lp);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }
}
